import socket
import threading

ip_port = ("192.168.43.218",8006)

class AcceptThread(threading.Thread):
    def __init__(self,conn):
        threading.Thread.__init__(self)
        self.conn = conn
    def run(self):
        while True:
            try:
                info = self.conn.recv(1024)
                if info:
                    info = str(info,encoding="utf-8")
                    print("对方:"+info)
                    if info == "exit":
                        break
                else:
                    break
            except:
                pass

        print("接受模块终止")


class SendThread(threading.Thread):
    def __init__(self,conn):
        threading.Thread.__init__(self)
        self.conn = conn

    def run(self):
        while True:
            msg = input("我: ").strip()
            try:
                self.conn.sendall(bytes(msg,encoding="utf-8"))
            except:
                break
            if msg == 'exit':

                break
        print("发送模块终止")
def start():
    sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)
    sk.bind(ip_port)
    sk.listen(10)
    print("Connecting...")
    conn,addr  =sk.accept()
    print("Connected with %s"%(str(addr)))
    print("Starting...")


    accept_thread = AcceptThread(conn)
    send_thread = SendThread(conn)

    accept_thread.start()
    send_thread.start()


    send_thread.join()
    send_thread.join()


start()

